﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SharePointUtilitiesForDevelopers.Tests
{
    /// <summary>
    /// Test class for <see cref="SharePointXml"/>.
    /// </summary>
    [TestClass]
    public class SharePointQueryOrderByClauseTest
    {
        [TestMethod]
        public void AddExpression_should_add_correctly()
        {
            var clause = new SharePointQueryOrderByClause();
            Assert.AreEqual(0, clause.Expressions.Count);
            clause.AddExpression("Title", false);
            Assert.AreEqual(1, clause.Expressions.Count);
            Assert.AreEqual("Title", clause.Expressions[0].FieldStaticName);
            Assert.AreEqual(false, clause.Expressions[0].SortAscending);

        }

        [TestMethod]
        public void AddExpression_should_add_correctly_when_sort_direction_is_not_specified()
        {
            var clause = new SharePointQueryOrderByClause();
            Assert.AreEqual(0, clause.Expressions.Count);
            clause.AddExpression("Title");
            Assert.AreEqual(1, clause.Expressions.Count);
            Assert.AreEqual(true, clause.Expressions[0].SortAscending);
        }

        [TestMethod]
        public void AddExpression_should_replace_expression_when_field_already_exists()
        {
            var clause = new SharePointQueryOrderByClause();
            clause.AddExpression("Title", true);
            Assert.AreEqual(1, clause.Expressions.Count);
            Assert.AreEqual(true, clause.Expressions[0].SortAscending);
            clause.AddExpression("Title", false);
            Assert.AreEqual(1, clause.Expressions.Count);
            Assert.AreEqual(false, clause.Expressions[0].SortAscending);
        }

        [TestMethod]
        public void ContainsField_returns_true_when_field_contained_by_expression()
        {
            var clause = new SharePointQueryOrderByClause();
            clause.AddExpression("Title", true);
            Assert.IsTrue(clause.ContainsFieldId("Title"));
        }

        [TestMethod]
        public void ContainsField_returns_false_when_field_static_name_is_null()
        {
            var clause = new SharePointQueryOrderByClause();
            clause.AddExpression("Title", true);
            Assert.IsFalse(clause.ContainsFieldId(null));
        }

        [TestMethod]
        public void ContainsField_returns_false_when_field_static_name_is_empty_string()
        {
            var clause = new SharePointQueryOrderByClause();
            clause.AddExpression("Title", true);
            Assert.IsFalse(clause.ContainsFieldId(string.Empty));
        }

    }
}
